using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using TidhiGanitam.DataModel;
using TidhiGanitam.Ganitam;

namespace TidhiGanitam
{
    public partial class GetIyanamsaDetails : Form
    {
        private YearDetails m_YearDetails;

        public YearDetails UgadiDetails
        {
            get
            { return m_YearDetails; }
        }
        public GetIyanamsaDetails()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (isValidInput())
            {
                m_YearDetails.UgadiDate = dt_Ugadi.Value;
                m_YearDetails.Year = dt_Ugadi.Value.Year;
                m_YearDetails.UgadiIyanamsamu = new Rasi(
                    0,
                    Convert.ToDouble(txtBhaga.Text),
                    Convert.ToDouble(txtLipta.Text),
                    Convert.ToDouble(txtVilipta.Text)
                );
                m_YearDetails.AdhikaMasamu = Convert.ToInt32(ddAdhikaMasamu.SelectedValue) - 1;
                DialogResult = DialogResult.OK;
                Close();
            }
        }

        private bool isValidInput()
        {
            bool retValue = true;
            string ErrorString = string.Empty;
            int iRasi = 0;
            int iBhaga = 0;
            int iLipta = 0;
            int iVilipta = 0;
            try
            {
                iBhaga = Convert.ToInt32(txtBhaga.Text);
                iLipta = Convert.ToInt32(txtLipta.Text);
                iVilipta = Convert.ToInt32(txtVilipta.Text);
            }
            catch
            {
                ErrorString += "Invalid Input for Iyanamsa Value. \n";
            }
            if (iRasi < 0 || iRasi > 11)
            {
                ErrorString += "Invalid value for Rasi. \n";
            }
            if (iBhaga < 0 || iBhaga > 59)
            {
                ErrorString += "Invalid value for Bhaga. \n";
            }
            if (iLipta < 0 || iLipta > 59)
            {
                ErrorString += "Invalid value for Lipta. \n";
            }
            if (iVilipta < 0 || iVilipta > 59)
            {
                ErrorString += "Invalid value for Vilipta. \n";
            }
            if (iRasi == 0 && iBhaga == 0 && iLipta == 0 && iVilipta == 0)
            {
                ErrorString += "Invalid Iyanamsa value. \n";
            }
            if (ErrorString.Length > 0)
            {
                MessageBox.Show(ErrorString);
                retValue = false;
            }
            return retValue;
        }

        private void GetIyanamsaDetails_Load(object sender, EventArgs e)
        {
            m_YearDetails = new YearDetails();
            ddAdhikaMasamu.DataSource = Enum.GetValues(typeof(Enums.Masamulu));
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }
    }
}